package com.softa.service.business.impl;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageInfo;
import com.softa.annotation.ServiceLog;
import com.softa.bean.DataGridModel;
import com.softa.dao.mapper.base.ProductTypeMapper;
import com.softa.dao.mapper.relation.business.ProductTypeRelationMapper;
import com.softa.dao.model.ProductType;
import com.softa.service.business.ProductTypeService;
import com.softa.vo.DataGridRequest;
import com.softa.vo.QueryRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Resource
    private ProductTypeMapper productTypeMapper;
    @Resource
    private ProductTypeRelationMapper productTypeRelationMapper;

    @ServiceLog(value="创建产品类型")
    public String createProductType(ProductType productType) {
        DataGridModel dataGrid = new DataGridModel();
        dataGrid.getParams().put("code", productType.getCode());
        boolean isName=this.isExists(dataGrid);
        String str="录入成功";
        if(isName){
            str="1";//存在名称
        }else{
            productType.setCreateTime(new Date());
            productType.setUpdateTime(new Date());
            productType.setDeleted(false);
            productType.setVersion((productType.getVersion() == null?0:productType.getVersion())+1);
            productTypeMapper.insert(productType);
        }
        return str;
    }

    @ServiceLog(value="修改产品类型")
    public String updateType(ProductType productType) {
        DataGridModel dataGrid = new DataGridModel();
        dataGrid.getParams().put("id", productType.getId());
        dataGrid.getParams().put("code", productType.getCode());
        boolean isCode = this.isExists(dataGrid);
        String str = "修改成功";
        if (isCode) {
            str = "1";//存在名称
        } else {
            productType.setUpdateTime(new Date());
            productTypeRelationMapper.updateByPrimaryKeySelective(productType);
        }
        return str;
    }

    @ServiceLog(value="删除产品类型")
    public void deleteType(Long id) {
        ProductType productType = productTypeMapper.selectByPrimaryKey(id);
        productType.setUpdateTime(new Date());
        productType.setDeleted(true);
        productTypeRelationMapper.updateByPrimaryKeySelective(productType);
    }

    @Override
    public PageInfo<ProductType> queryProductTypeByParam(DataGridRequest request, final QueryRequest queryRequest) {
	  PageInfo<ProductType> page = request.startPagePlugin().doSelectPageInfo(new ISelect() {
		@Override
		public void doSelect() {
		    productTypeRelationMapper.selectByCondition(queryRequest);
		}
	  });
	  return  page;
    }

    public boolean isExists(DataGridModel dataGrid){
        List<ProductType> list=productTypeRelationMapper.selectByParam(dataGrid);
        boolean flag=false;
        if(list.size() > 0){
            flag=true;
        }
        return flag;
    }

    @Override
    public List<ProductType> queryAll() {
        QueryRequest queryRequest = new QueryRequest();
        return  productTypeRelationMapper.selectByCondition(queryRequest);
    }

}
